import type { AskLeave } from "@/types/common";

//按照学院进行分组展示
export const groupAskLeaveList= (list:AskLeave[])=>{

    if(!isNeedGroup(list)){
        return list;
    }

    const result:AskLeave[] = [];

    var lastCollege:string = '';
    var hasGroup = false;

    list.forEach((item)=>{
        if(lastCollege == ''){
            lastCollege = item.college_name;
        }

        if(item.college_name != lastCollege){

            if(item.groupFlag){
                hasGroup = true;

            }else if (hasGroup){
                lastCollege = item.college_name;
                hasGroup = false;
            }else{
                lastCollege = item.college_name;
                result.push({
                    ...item,
                    college_name:item.college_name+'任职期间的请假记录',
                    groupFlag:true
                })
            }            
        }

        result.push(item);

    })

    return result;
}

//是否需要分组
export const isNeedGroup = (list:AskLeave[])=>{
     var result = true;

    var staff:string = '';

    list.forEach((item)=>{
        if(staff == ''){
            staff = item.user_id;
        }

        if(item.user_id != staff){ //有多个人
           result = false;
        }

    })

    return result;
};